package com.notebad.ui;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.notebad.R;
import com.notebad.note.Note;
import com.notebad.note.NotesDbManager;

public class NoteList extends ListActivity {
	
	private static final String TAG = "NoteList";
    
    private static final int EDIT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private static final int QUIT_ID = Menu.FIRST + 2;

    private NotesDbManager mDbHelper;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        mDbHelper = new NotesDbManager(this);
        mDbHelper.open();
        
        //TODO zaper la debug note
       Note debugNote = new Note(0, "8 May 2010", "Work", "Note 1", "Ludo a un gros boule", 49.4, 2.8, "Compiegne");
       Note debugNote2 = new Note(0, "8 May 2010", "Home", "Note 2", "Alex est costaud", 49.4, 2.8, "Compiegne");
       
       mDbHelper.createNote(debugNote);
       mDbHelper.createNote(debugNote2);
        
        fillData();
        registerForContextMenu(getListView());
    }
    
    private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);
        
        // Create an array to specify the fields we want to display in the list (TITLE, DATE and ADDRESS)
        String[] from = new String[]{NotesDbManager.KEY_TITLE, NotesDbManager.KEY_DATE, NotesDbManager.KEY_ADDRESS};
        
        // and an array of the fields we want to bind those fields to
        int[] to = new int[]{R.id.text1, R.id.text2, R.id.text3};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
        setListAdapter(notes);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.ic_menu_edit);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete).setIcon(R.drawable.ic_menu_delete);
        menu.add(0, QUIT_ID, 0, R.string.menu_quit).setIcon(R.drawable.ic_menu_close_clear_cancel);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case EDIT_ID:
            createNote();
            return true;
        case QUIT_ID:
        	Log.i(TAG, "Quit NoteBad apps");
            finish();
            return true;
        case DELETE_ID:
        	mDbHelper.deleteNote(getSelectedItemId());
	        fillData();
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
	
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete).setIcon(R.drawable.ic_menu_delete);
        menu.add(0, EDIT_ID, 0, R.string.menu_edit).setIcon(R.drawable.ic_menu_edit);
	}

    @Override
	public boolean onContextItemSelected(MenuItem item) {
		switch(item.getItemId()) {
    	case DELETE_ID:
//    		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//	        mDbHelper.deleteNote(info.id);
//	        fillData();
	        return true;
    	case EDIT_ID:
    		//TODO
    		return true;
		}
		return super.onContextItemSelected(item);
	}
	
    private void createNote() {
        Intent i = new Intent(this, NoteBad.class);
        startActivity(i);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        //TODO
        Note note = mDbHelper.fetchNote(id);
        Log.d(TAG, note.toString() + "/" + note.getDate() + "/");
        
//        Intent i = new Intent(this, NoteEditer.class);
//        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
//        startActivityForResult(i, ACTIVITY_EDIT);
        
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}